#define NLEDs 6
#define CenterWidth 2
int LEDChannels2[NLEDs * 2][3] = {0};
// these are small so i'll leave them in RAM, larger data should be moved to PROGMEM
int KittLeadingEdge[] = {600, 400, 200};
int KittTrailingEdge[] = {200, 400, 600};

int KittLeadSize = sizeof(KittLeadingEdge) / sizeof(int);
int KittTrailSize = sizeof(KittTrailingEdge) / sizeof(int);
int KittTotalSize = KittLeadSize + KittTrailSize + CenterWidth;

void WriteLEDArray2()
    {
 
    SB_CommandMode = B00;
    for (int h = 0;h<NLEDs*2;h++) {
	  SB_RedCommand = LEDChannels2[h][0];
	  SB_GreenCommand = LEDChannels2[h][1];
	  SB_BlueCommand = LEDChannels2[h][2];
	  SB_SendPacket();
    }
 
    delayMicroseconds(15);
    digitalWrite(latchpin,HIGH);
    delayMicroseconds(15);
    digitalWrite(latchpin,LOW);
 
    SB_CommandMode = B01;
    for (int z = 0; z < NLEDs*2; z++) SB_SendPacket();
    delayMicroseconds(15);
    digitalWrite(latchpin,HIGH);
    delayMicroseconds(15);
    digitalWrite(latchpin,LOW); 
}

// Re-use my old array rotating code, but use collapsed fake array to save memory
// (at expense of CPU)
int KittFakeArray(int index) {

 if (index >= 0 && index < KittTrailSize) {
     return KittTrailingEdge[index];
 } else if (index >= KittTrailSize && index < (CenterWidth + KittTrailSize)) {
     return 1023;
 } else if (index >= (CenterWidth + KittTrailSize) && index < KittTotalSize) {
     return KittLeadingEdge[index-CenterWidth-KittTrailSize];
 } else {
     return 0;
 }
 
}
 

void NewKittFill(int dir, int FillSpeed, int FadeSteps, int fromred, int fromgreen, int fromblue, int tored, int togreen, int toblue) {

 int KittFadeFactor = 0;
 int revIndex;
 
 
 for (int FillCount = 0; FillCount < (KittTotalSize+NLEDs); FillCount++) {
 
   for (int FadeCount = 0; FadeCount < FadeSteps; FadeCount++) {
   
     for (int i = 0; i < NLEDs; i++) {
       
       KittFadeFactor = ((KittFakeArray(KittTotalSize - FillCount + i)) * (FadeSteps - FadeCount - 1) + (KittFakeArray(KittTotalSize - FillCount - 1 + i)) * (FadeCount)) / (2 * FadeSteps);

       if (dir == 0) {
         
          for (int r = 0; r < 33; r++) {

         LEDChannels2[i][0] = (long) (fromred * (32 - r) + tored * r * KittFadeFactor) / 32; 
         LEDChannels2[i][1] = (long) (fromgreen * (32 - r) + togreen * r * KittFadeFactor)/ 32;
         LEDChannels2[i][2] = (long) (fromblue * (32 - r) + toblue * r * KittFadeFactor)/ 32;

         revIndex = NLEDs * 2 - i - 1;
         
         LEDChannels2[revIndex][0] = LEDChannels2[i][0];
         LEDChannels2[revIndex][1] = LEDChannels2[i][1];
         LEDChannels2[revIndex][2] = LEDChannels2[i][2];}
       } else {
         
         for (int r = 0; r < 33; r++) {
         revIndex = NLEDs - 1 - i;
         
         LEDChannels2[revIndex][0] = (long) tored * KittFadeFactor / 1023; 
         LEDChannels2[revIndex][1] = (long) togreen * KittFadeFactor / 1023;
         LEDChannels2[revIndex][2] = (long) toblue * KittFadeFactor / 1023;


         LEDChannels2[NLEDs + i][0] = LEDChannels2[revIndex][0]; 
         LEDChannels2[NLEDs + i][1] = LEDChannels2[revIndex][1];
         LEDChannels2[NLEDs + i][2] = LEDChannels2[revIndex][2];}
       }
   
     }
   
     WriteLEDArray2();
     delay(FillSpeed);
   
   }
   
 }
 
}

void KRider() {

 NewKittFill(0, 10, 7, 200, 0, 0, 0, 0, 200);
 delay(300);
 NewKittFill(1, 10, 7, 0, 200, 0, 200, 200, 200); 
} 
